#include <stddef.h>
#include <stdio.h>

size_t strlen(const char *str)
{
	if (str == NULL)
		return 0;

	size_t counter = 0;
	while (*str++)
		++counter;
	return counter;
}

char *strchr(const char *str, int ch)
{
	if (str == NULL)
		return (char*)str;

	while (*str && *str != ch)
		++str;
	if (*str == ch)
		return (char*)str;
	return NULL;
}

int strncmp(const char *lhs, const char *rhs, size_t count)
{
	if (count == 0)
		return 0;

	while (*lhs == *rhs)
	{
		if (*lhs == '\0' \
			|| --count == 0)
			return 0;
		++lhs, ++rhs;
	}
	return *lhs - *rhs > 0 ? 1 : -1;
}

char *strstr(const char *str, const char *sub)
{
	size_t count = strlen(sub);
	if (count == 0)
		return NULL;

	for (; str = strchr(str, *sub); ++str)
		if (strncmp(str, sub, count) == 0)
			return (char*)str;
	return NULL;
}

int main()
{
	printf("0x%p\n", strstr("freedom and justice", " and "));
	return 0;
}
